#include "title.h"
#include <QMessageBox>

// Title 构造函数，初始化主菜单界面（登录、注册、游客进入等）
Title::Title(View *view, QWidget *parent) : QGraphicsScene(parent) {

    viewer = view;
    viewer->setViewportUpdateMode(QGraphicsView::SmartViewportUpdate);

    // 滚动条
    scroll = new QScrollBar;
    scroll = viewer->horizontalScrollBar();

    // 背景动画设置（背景图从左滑动到右）
    background = new AnimatedGraphicsItem;
    background->setPixmap(QPixmap(":/images/background.png"));
    foreground = new QGraphicsPixmapItem(QPixmap(":/images/backgroundpic.jpg"));
    foreground->setScale(qMin((qreal)width / foreground->pixmap().width(),
                              (qreal)height / foreground->pixmap().height()));
    logo = new QGraphicsPixmapItem(QPixmap(":/images/gamelogo.png"));
    logo->setOpacity(0.5);  // 0.0 完全透明，1.0 完全不透明

    // 设置背景滚动动画
    animation = new QPropertyAnimation(background, "pos");
    animation->setLoopCount(-1);                   // 无限循环
    animation->setDuration(150000);                // 持续时间
    animation->setStartValue(QPoint(-width, 0));   // 起始位置
    animation->setEndValue(QPoint(0, 0));          // 结束位置
    animation->start();

    logo->setScale(0.5); // 先缩放

    qreal scaledWidth = logo->boundingRect().width() * 0.5;

    logo->setPos((width - scaledWidth) / 2, height / 12);

    // 添加图元到场景中
    addItem(background);
    addItem(foreground);
    addItem(logo);

    this->setFocus();
    this->setSceneRect(0, 0, 1280, 720);           // 场景尺寸
    view->sceneSet(this);                          // 将此场景设置为当前场景

    // ========== 以下为 UI 控件区域 ==========

    // 游客登录按钮
    developerButton = new QPushButton(viewer);
    developerButton->setText("跃迁开始");
    developerButton->setObjectName("developerButton");
    developerButton->setToolTip("Login as a guest");
    developerButton->setGeometry(QRect(490, 400, 150, 200));
    connect(developerButton, SIGNAL(clicked()), this, SLOT(developerLogin()));
    // 透明化
    developerButton->setStyleSheet("background-color: rgba(255, 255, 255, 50); border: none;");

    // 退出按钮
    quitButton = new QPushButton(viewer);
    quitButton->setText("下次一定");
    quitButton->setObjectName("quitButton");
    quitButton->setToolTip("Quit program");
    quitButton->setGeometry(QRect(642, 400, 150, 200));
    connect(quitButton, SIGNAL(clicked()), this, SLOT(quitProgram()));
    // 透明化
    quitButton->setStyleSheet("background-color: rgba(255, 255, 255, 50); border: none;");

}

// ✅ 退出按钮功能：关闭程序
void Title::quitProgram() {
    qApp->quit();
}

// ✅ 游客登录（跳转到游戏主场景）
void Title::developerLogin() {
    developerButton->close();
    quitButton->close();

    // 创建游戏主场景并切换视图
    scene = new MyScene(scroll, this);
    viewer->sceneSet(scene);

}
